/**
 * May 12, 2014 3:16:05 PM
 */
package elitech.vietnam.copyweatherfashion.adapters;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import elitech.vietnam.copyweatherfashion.MainActivity;
import elitech.vietnam.copyweatherfashion.R;
import elitech.vietnam.copyweatherfashion.config.Const;
import elitech.vietnam.copyweatherfashion.entities.Post;

/**
 * @author Cong
 *
 */
public class ShopRecentAdapter extends ArrayAdapter<Post> {

	int mItemHeight;
	
	public ShopRecentAdapter(Context context, int resource, List<Post> objects) {
		super(context, resource, objects);
		mItemHeight = ((MainActivity) context).mConfig.getScreenHeigh()/4;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ViewHolder holder;
		
		if (row == null) {
			row = LayoutInflater.from(getContext()).inflate(R.layout.layout_recentitem, parent, false);
			
			holder = new ViewHolder();
			holder.mImage = (ImageView) row.findViewById(R.id.recenitem_imgContent);
			holder.mTxtDate = (TextView) row.findViewById(R.id.recenitem_txtDate);
			
			LayoutParams params = holder.mImage.getLayoutParams();
			params.height = mItemHeight;
			holder.mImage.setLayoutParams(params);
			row.setTag(holder);
		} else 
			holder = (ViewHolder) row.getTag();
		
		final Post mItem = getItem(position);
		UrlImageViewHelper.setUrlDrawable(holder.mImage, Const.SERVER_IMAGE_URL + mItem.Image);
		holder.mTxtDate.setText(new SimpleDateFormat("MM/dd", Locale.getDefault()).format(mItem.Date()));
		
		return row;
	}

	static class ViewHolder {
		ImageView mImage;
		TextView mTxtDate;
	}
}
